/*
 * Write a program that reads a real number x
 * and calculates the following sum:
 * S = 1 + x/1! + x^2/2! + .. + x^n/n!
 */

package _3;

import java.util.Scanner;

public class _15 {
	public static long fac (int n) {
		int res = 1;

		if (n < 0)
			n = -n;

		while (n > 1) {
			res *= n--;
		}

		return res;
	}

	public static void main (String[] args) {
		Scanner scn = new Scanner (System.in);
		int n = 0, x = 0;

		System.out.print ("n ?= ");
		if (scn.hasNextInt ())
			n = scn.nextInt ();
		else {
			System.err.println ("error: mismatched input");
			System.exit (1);
		}

		if (n < 0) {
			System.err.println ("error: n should not be a negative number");
			System.exit (1);
		}

		System.out.print ("x ?= ");
		if (scn.hasNextInt ())
			x = scn.nextInt ();
		else {
			System.err.println ("error: mismatched input");
			System.exit (1);
		}

		double sum = 1d;
		for (int i = 1; i <= n; i++)
			sum += Math.pow (x, i) / fac (i);

		System.out.printf ("sum = %g\n", sum);
	}
}